package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.WctFstInfo;
import cc.rengu.redp.bizimpl.upmp.service.WctFstInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:微信一级类目管理")
@RestController
@RequestMapping("/admin/merParam/wctFstInfo")
public class WctFstInfoController extends BaseController {
    @Autowired
    private WctFstInfoService wctFstInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, WctFstInfo entity) {
        return getDataTable(this.wctFstInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public WctFstInfo getOne(@NotBlank(message = "{required}") String id) {
        return wctFstInfoService.getById(id);
    }
    @Log("根据一级类目代码查询单条数据")
    @GetMapping(value = "/findOneByCode")
    @ApiImplicitParam(paramType = "query", name = "code", value = "一级类目代码", required = true, dataType = "String")
    public WctFstInfo findOneByCode(@NotBlank(message = "{required}") String code) {
        LambdaQueryWrapper<WctFstInfo> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(WctFstInfo::getWctFstNo,code);
//        wrapper.eq(WctFstInfo::getStatus,BusinessContainer.data_sta_00);
        return wctFstInfoService.getOne(wrapper);
    }
    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "wctFstNo", value = "微信商户一级类目代码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "wctFstName", value = "微信商户一级类目描述", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "wctFstType", value = "商户主体", required = true, dataType = "String")
    })
    public void add(@Valid WctFstInfo entity) throws RedpException {
        try {
            LambdaQueryWrapper<WctFstInfo> wrapper = new LambdaQueryWrapper();
            wrapper.eq(WctFstInfo::getWctFstNo, entity.getWctFstNo());
            WctFstInfo info = wctFstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 一级类目代码 ] 重复，请重新输入");
            }
            wrapper = new LambdaQueryWrapper();
            wrapper.eq(WctFstInfo::getWctFstName, entity.getWctFstName());
            info = wctFstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 一级类目描述 ] 重复，请重新输入");
            }
            this.wctFstInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid WctFstInfo entity) throws RedpException {
        try {
            LambdaQueryWrapper<WctFstInfo> wrapper = new LambdaQueryWrapper();
            wrapper.eq(WctFstInfo::getWctFstNo, entity.getWctFstNo());
            wrapper.ne(WctFstInfo::getId, entity.getId());
            WctFstInfo info = wctFstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 一级类目代码 ] 重复，请重新输入");
            }
            wrapper = new LambdaQueryWrapper();
            wrapper.eq(WctFstInfo::getWctFstName, entity.getWctFstName());
            wrapper.ne(WctFstInfo::getId, entity.getId());
            info = wctFstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 一级类目描述 ] 重复，请重新输入");
            }
            this.wctFstInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            WctFstInfo entity = wctFstInfoService.getById(id);
            String isExist = wctFstInfoService.isExistSubDirect(entity.getWctFstNo());
            if (isExist.equals("Y")) {
                throw new RedpException("[ 微信商户一级类目下存在二级类目，不允许注销 ] 重复，请重新输入");
            }
            this.wctFstInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.wctFstInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
    @GetMapping("/wctfstSelectAll")
    public List<String> wctfstSelectAll() {
        return this.wctFstInfoService.findAllWctFstNo();
    }
}
